package org.brownflat.watergate.dao;

import org.brownflat.watergate.domain.ConfigurationKey;
import org.brownflat.watergate.domain.Configuration;
import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import static org.hibernate.criterion.Restrictions.eq;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
@Repository
public class ConfigurationDAO extends AbstractHibernateDAO<Configuration> {

    public ConfigurationDAO() {
        super(Configuration.class);
    }

    public Configuration findByKey(ConfigurationKey configurationKey) {
        Criteria criteria = getCriteria().add(eq("configurationKey", configurationKey));
        return (Configuration) criteria.uniqueResult();
    }

    public void createFor(ConfigurationKey key) {
        Configuration configuration = new Configuration();
        configuration.setConfigurationKey(key);
        configuration.setConfigurationValue(key.getDefaultValue());
        configuration.setValueType(key.getType().getName());
        upsert(configuration);
    }
}
